package com.ifyyf.controller;

import com.ifyyf.pojo.Dept;
import com.ifyyf.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author if
 * @Description: What is it
 * @Date 2021-08-24 下午 12:13
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;
    /**
     * 导入这个包org.springframework.cloud.client.discovery.DiscoveryClient
     */
    @Autowired
    private DiscoveryClient client;

    @GetMapping("/get/{deptno}")
    public Dept get(@PathVariable("deptno") Long deptno){
        return deptService.getById(deptno);
    }

    @GetMapping("/getAll")
    public List<Dept> getAll(){
        return deptService.getAll();
    }

    /**
     * 根据DiscoveryClient类可以获取注册中心注册的微服务的一些信息
     * @return
     */
    @GetMapping("/discovery")
    public Object discovery(){
        //获取微服务列表清单
        List<String> services = client.getServices();
        System.out.println("discovery =>services: "+services);
        //根据具体的微服务id(ApplicationName)，获取实例对象
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println("--------------------");
            System.out.println("instance.getHost() = "+instance.getHost());
            System.out.println("instance.getPort() = "+instance.getPort());
            System.out.println("instance.getUri() = "+instance.getUri());
            System.out.println("instance.getServiceId() = "+instance.getServiceId());
        }
        return this.client;
    }
}
